package com.ecp.pay.online.service;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.ServletRequestUtils;

import beartool.MD5;

import com.ecp.base.service.UserService;
import com.ecp.crm.bean.User;
import com.ecp.pay.online.config.OnlineConfig;
import com.ecp.util.Util;
import com.ecp.util.WebUtil;

/**
 * * @author shichao;
 * 
 * @date 2015年11月4日 上午10:40:21
 */
@Repository("wangyinService")
public class WangyinServiceImpl implements WangyinService {

	@Autowired
	private WangyinBankService wangyinBankService;
	@Autowired
	private UserService userService;

	
	@Override
	public Map<String, Object> getSendInfo(HttpServletRequest request) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		String orderName = "";
		User user = WebUtil.getFrontUser(request, userService);
		if (user == null) {
			model.put("error", true);
			model.put("msg", "用户未登录");
			return model;
		}

		String bankId = ServletRequestUtils.getStringParameter(request, "bank", "");
		String aliasCode = ServletRequestUtils.getStringParameter(request, "aliasCode", "");

		String orderIds = aliasCode;
//		FlightOrder order = this.flightOrderService.getEntity(new String[] { "aliasCode" },
//				new Object[] { aliasCode });
//		if (order == null) {
//			model.put("error", true);
//			model.put("msg", "该订单未找到！");
//			return model;
//		}
//		if (order.getUserId() != user.getId()) {
//			model.put("error", true);
//			model.put("msg", "该订单不属于当前登录用户");
//			return model;
//		}
		double totalPrices = 0;//this.flightOrderService.getOrderPay(order);
//		boolean isCancel = this.flightOrderService.isCancelByOrder(order);
//		if (isCancel) {
//			model.put("error", true);
//			model.put("msg", "当前订单不可支付，如有疑问请联系品质365");
//			return model;
//		}
		
		//支付前再次查询政策加密串
		try{
//			FlightOrderExt orderExt = flightOrderExtService.getOrderExtById(order.getId());
//			if(orderExt != null){
//				Map<String, String> policy = callYeekingIBEService.callAirPolicy(orderExt.getEncryptString());
//				if(orderExt.getIbeprice() == Double.valueOf(policy.get("ibePrice"))){
//					orderExt.setPolicyEncryptString( policy.get("ariEncryptString"));
//					Map<String, String> policyC = callYeekingIBEService.callAirPolicy(orderExt.getChildEncryptString());
//					if(Double.valueOf(policyC.get("ibePrice")) / 2 == orderExt.getChildprice() ) {
//						orderExt.setChildPolicyEncryptString(policyC.get("ariEncryptString"));
//					}else{
//						model.put("error", true);
//						model.put("msg", "您所订票已过期，请重新预订！");
//						return model;
//					}
//				}else{
//					model.put("error", true);
//					model.put("msg", "您所订票已过期，请重新预订！");
//					return model;
//				}
//				flightOrderExtService.updateOrderExt(orderExt);
//			}
		}catch(Exception e){
			
		}
		
		
//		String depName = flightCityService.getCityNameByCode(order.getDepCode());
//		String arrName = flightCityService.getCityNameByCode(order.getArrCode());
//		orderName += depName + "飞至" + arrName + " 编号为" + aliasCode;
		// 商家用户名
		String v_mid = OnlineConfig.v_mid;

		// 商家流水号
		String v_oid = aliasCode;
		// 密钥
		String key = OnlineConfig.key;
		// 支付金额以元为单位计算所以除以100
		String v_amount = totalPrices + "";
//		v_amount = "0.01";
		// 币种类型
		String v_moneytype = OnlineConfig.moneyType;
		// 支付成功后用户点击展示的url
		String v_url = OnlineConfig.backShowUrl;
		// 选择支付的银行
		String pmode_id = bankId;
		// 自定义的参数
		String pay_object_type = OnlineConfig.Pay_Object_Order;
		String remark1 = pay_object_type + "@" + orderIds;
		// 异步通知Url
		String remark2 = OnlineConfig.receiveUrl;
		// md5校验参数
		String v_md5info = v_amount + v_moneytype + v_oid + v_mid + v_url + key;

		MD5 md5 = new MD5();
		v_md5info = md5.getMD5ofStr(v_md5info).toUpperCase();

		model.put("v_mid", v_mid);
		model.put("v_oid", v_oid);
		model.put("v_amount", v_amount);
		model.put("v_moneytype", v_moneytype);
		model.put("v_url", v_url);
		model.put("pmode_id", pmode_id);
		model.put("remark1", remark1);
		model.put("remark2", remark2);
		model.put("v_md5info", v_md5info);
		model.put("isWang", 1);

		/** 非基础信息 **/
		model.put("v_rcvname", Util.cutStr(user.getLoginId(), 37));
		model.put("v_rcvaddr", Util.cutStr(user.getHomeAddress(), 97));
		model.put("v_rcvtel", Util.cutStr(user.getHomePhone(), 97));
		model.put("v_rcvpost", user.getPostalCode());
		model.put("v_rcvemail", user.getEmail());
		model.put("v_rcvmobile", user.getMobilePhone());
		model.put("v_ordername", Util.cutStr(orderName, 37));
		model.put("v_orderaddr", Util.cutStr(user.getHomeAddress(), 97));
		model.put("v_ordertel", user.getMobilePhone());
		model.put("v_orderpost", "");
		model.put("v_orderemail", user.getEmail());
		// model.put("v_ordermobile", order.getMailPhone());

		return model;
	}

}
